package com.glsc.ngateway.oaflow.amqp;

import com.alibaba.fastjson.JSONObject;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.TRPARequest;
import com.glsc.ngateway.common.base.repo.mysql.gateway.oaflow.TOaFlowMessageTableRepository;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.TRPARequestRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class RPAOaReceiver {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final String rpaQueue = "gateway.queue.fwFlow.rpa";
    @Autowired
    TOaFlowMessageTableRepository tOaFlowMessageTableRepository;

    @Autowired
    TRPARequestRepository trpaRequestRepository;


    /**
     * JSON格式：workflowId:111,requestId:123456789,workflowName:测试流程标题,description:success
     */
    @RabbitListener(queues = {rpaQueue})
    @RabbitHandler
    public void processMessage(Message messageObject) throws IOException, NoSuchFieldException, IllegalAccessException {
        try {
            String message = new String(messageObject.getBody());
            logger.info("gateway.queue.fwFlow.rpa,消息队列收到消息，message receiver:" + message);
            JSONObject msgJsonObject = JSONObject.parseObject(message);
            String workflowId = msgJsonObject.getString("workflowId");
            String requestId = msgJsonObject.getString("requestId");
            String workflowName = msgJsonObject.getString("workflowName");
            String description = msgJsonObject.getString("description");

            TRPARequest trpaRequest = new TRPARequest();
            trpaRequest.setStatus("0");
            trpaRequest.setRequestId(requestId);
            trpaRequest.setWorkFlowId(workflowId);
            trpaRequest.setWorkFlowName(workflowName);
            trpaRequestRepository.save(trpaRequest);

        } catch (Exception e) {
            logger.error("RPA，消费消息队列异常", e);
        }
    }


}
